<template>
  <div class="app-container">
    <div>
      <el-button type="success" size="mini" @click="showAddDialog()">添加顶级菜单</el-button>
    </div>
    <el-table
      :data="permissionVOList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="icon" label="图表">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="权限类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type==0">目录权限</el-tag>
          <el-tag v-if="scope.row.type==1" type="success">菜单权限</el-tag>
          <el-tag v-if="scope.row.type==2" type="warning">按钮权限</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="path" label="路由地址"></el-table-column>
      <el-table-column prop="component" label="组件路径"></el-table-column>
      <el-table-column prop="permissionValue" label="权限值"></el-table-column>
      <el-table-column label="操作" width="180" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            icon="el-icon-plus"
            title="添加下级菜单"
            @click="showAddDialog(scope.row)"/>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            title="修改"
            @click="showUpdateDialog(scope.row)"/>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            title="删除"
            @click="deleteById(scope.row.id)"
            :disabled="scope.row.children.length > 0"/>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加、修改dialog -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%" >
      <el-form ref="dataForm" :model="permission" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="上级权限">
          <el-input v-model="permission.parentName" :disabled="true"/>
        </el-form-item>
        <el-form-item label="权限类型" prop="type">
          <el-radio-group v-model="permission.type" :disabled="typeDisabled">
            <el-radio :label="0" :disabled="type0Disabled">目录</el-radio>
            <el-radio :label="1" :disabled="type1Disabled">菜单</el-radio>
            <el-radio :label="2" :disabled="type2Disabled">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permission.name"/>
        </el-form-item>
        <el-form-item label="图标" prop="icon" v-if="permission.type !== 2">
          <el-select v-model="permission.icon" clearable>
            <el-option v-for="item in iconList" :key="item.class" :label="item.class" :value="item.class">
            <span style="float: left;">
             <i :class="item.class"></i>
            </span>
              <span style="padding-left: 6px;">{{ item.class }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="permission.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item prop="path"  v-if="permission.type !== 2">
          <span slot="label">
            <el-tooltip content="访问的路由地址，如：`rbac`" placement="top">
            <i class="el-icon-question"></i>
            </el-tooltip>
            路由地址
          </span>
          <el-input v-model="permission.path" placeholder="请输入路由地址" />
        </el-form-item>
        <el-form-item prop="component" v-if="permission.type !== 0 && permission.type !== 2">
            <span slot="label">
              <el-tooltip content="访问的组件路径，如：`rbac/user/list`，默认在`views`目录下" placement="top">
              <i class="el-icon-question"></i>
              </el-tooltip>
              组件路径
            </span>
          <el-input v-model="permission.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item v-if="permission.type === 2">
          <el-input v-model="permission.permissionValue" placeholder="请输入权限标识" maxlength="100" />
          <span slot="label">
            <el-tooltip content="按钮权限，如：('rbac:user:add')" placement="top">
            <i class="el-icon-question"></i>
            </el-tooltip>
            权限字符(按钮权限)
          </span>
        </el-form-item>
        <el-form-item label="状态" prop="type">
          <el-radio-group v-model="permission.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="addOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
<script>
//引入permission.js接口
import permissionApi from "@/api/rbac/permission";

const defaultPermission = {
  id: '',
  parentId: '',
  name: '',
  type: 0,
  path: '',
  component: '',
  permissionValue: '',
  icon: '',
  sort: 1,
  status: 1
}

export default {
  name: "",
  data() {
    return {
      permissionVOList: [],

      dialogVisible: false, //添加、修改dialog
      permission: {}, // 添加、修改对应封装permission对象
      dialogTitle: '',
      typeDisabled: false,
      type0Disabled: false,
      type1Disabled: false,
      type2Disabled: false,
      iconList: [
        {
          class: "el-icon-s-tools",
        },
        {
          class: "el-icon-s-custom",
        },
        {
          class: "el-icon-setting",
        },
        {
          class: "el-icon-user-solid",
        },
        {
          class: "el-icon-s-help",
        },
        {
          class: "el-icon-phone",
        },
        {
          class: "el-icon-s-unfold",
        },
        {
          class: "el-icon-s-operation",
        },
        {
          class: "el-icon-more-outline",
        },
        {
          class: "el-icon-s-check",
        },
        {
          class: "el-icon-tickets",
        },
        {
          class: "el-icon-s-goods",
        },
        {
          class: "el-icon-document-remove",
        },
        {
          class: "el-icon-warning",
        },
        {
          class: "el-icon-warning-outline",
        },
        {
          class: "el-icon-question",
        },
        {
          class: "el-icon-info",
        }
      ]
    };
  },
  created() {
    this.selectAll();
  },
  methods: {
    selectAll() {
      permissionApi.selectAll().then((response) => {
        this.permissionVOList = response.data
      });
    },
    deleteById(id) {
      this.$confirm("您确认要删除么？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        permissionApi.deleteById(id).then((response) => {
          if (response.ok) {
            this.$message({type: "success",message: response.msg});
            this.selectAll();
          } else {
            this.$message({type: "error", message: response.msg});
          }
        });
      });
    },
    showAddDialog(row) {
      this.dialogVisible = true;
      this.dialogTitle = '添加下级菜单'
      this.permission = Object.assign({}, defaultPermission)
      if (row) {
        this.permission.pid = row.id
        this.permission.parentName = row.name
        if (row.type === 0) {
          //这里只是默认是type=1，让菜单radio勾选，但是目录下面是可以有目录也可以有菜单
          this.permission.type = 1
          this.typeDisabled = false
          this.type0Disabled = false
          this.type1Disabled = false
          this.type2Disabled = true
        } else if (row.type === 1) {
          this.typeDisabled = true
          this.permission.type = 2
        }

      } else {//添加顶级菜单
        this.dialogTitle = '添加目录节点'
        this.typeDisabled = true
        this.permission.type = 0
        this.permission.component = 'Layout'
        this.permission.pid = 0
      }
    },
    showUpdateDialog(row) {
      this.dialogVisible = true;
      this.dialogTitle = '修改菜单'
      this.permission = Object.assign({}, row)
      this.typeDisabled = true
    },
    addOrUpdate() {
      if (!this.permission.id) {
        permissionApi.add(this.permission).then((response) => {
          if (response.ok) {
            this.$message({ type: "success", message: response.msg });
            this.dialogVisible = false;
            this.selectAll();
          }
        });
      } else {
        permissionApi.update(this.permission).then((response) => {
          if (response.ok) {
            this.$message({ type: "success", message: response.msg });
            this.dialogVisible = false;
            this.selectAll();
          }
        });
      }
    },
  },
};
</script>
 
<style scoped>
</style>